﻿using System;
using System.Web.Mvc;
using FBGraph.Web.Security;

namespace FBGraph.Web.Mvc
{
    /// <summary>An <see cref="ActionResult" /> returned from FBGraph.NET authorization requests when a request requires a user's authorization.</summary>
    public class RequiresAuthorizationResult : ActionResult, IFBGraphAuthorizationResultHandler
    {
        /// <summary>Initializes an instance of <see cref="RequiresAuthorizationResult" /> using the specified <see cref="ExtendedPermission" /> mapping.</summary>
        /// <param name="scope">A <see cref="ExtendedPermission" /> value mapping representing the extended permissions required for the original request.</param>
        public RequiresAuthorizationResult(ExtendedPermission scope)
        {
            this.Scope = scope;
        }

        /// <summary>Gets the <see cref="ExtendedPermission" /> value mapping representing the extended permissions required for the original request.</summary>
        public ExtendedPermission Scope { get; private set; }

        /// <summary>Redirects the user to authorize the application and/or authorize extended permissions after failing authorization.</summary>
        /// <param name="context">The current <see cref="ControllerContext" />.</param>
        public override void ExecuteResult(ControllerContext context)
        {            
            this.ExecuteAuthorizationRedirect(context.HttpContext, this.Scope);
        }
    }
}